package Chapter6Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class WindowsCalculation extends JFrame implements ActionListener {
    public JTextField show;
    private JButton sign;
    public JPanel button;
    private String[] figure = {"√", "+/-", "←", "C", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};
    String firstInput, operator, secondInput = null;

    public WindowsCalculation() {
        super("Windows计算器");
        this.setBounds(200, 300, 500, 300);
        this.show = new JTextField(350);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        button = new JPanel();
        button.setLayout(new GridLayout(5, 4));
        for (int i = 0; i < figure.length; i++) {
            sign = new JButton(figure[i]);
            sign.setFont(new Font("黑体", Font.BOLD, 16));
            this.button.add(sign);
            this.sign.addActionListener(this);
        }
        this.show.setEditable(false);
        this.add(show, BorderLayout.NORTH);
        this.add(button);
        this.setVisible(true);
        //this.pack();
    }

    @Override
    public void actionPerformed(ActionEvent event) {

        try {
//event.getActionCommand().matches("[\\+\\-*/]{1}")
            while (event.getActionCommand().matches("[0123456789.]{1}")){
                this.show.setText(this.show.getText() + event.getActionCommand());
                this.show.setHorizontalAlignment(JTextField.RIGHT);   //设置对齐方式是右对齐
                break;
            }
            switch (event.getActionCommand()) {
               /* case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case ".":
                    this.show.setText(this.show.getText() + event.getActionCommand());
                    this.show.setHorizontalAlignment(JTextField.RIGHT);
                    break;*/
                case "√":
                    this.show.setText("" + Math.sqrt(Double.valueOf(this.show.getText())));
                    break;
                case "←":
                    if (this.show.getText().length() > 1) {
                        this.show.setText(show.getText().substring(0, show.getText().length() - 1));
                    } else {
                        this.show.setText("0");
                    }
                    break;
                case "+/-":
                    this.show.setText((0 - Double.valueOf(show.getText())) + "");
                    break;
                case "C":
                    this.show.setText("");
                    firstInput = null;
                    secondInput = null;
                    operator = null;
                    break;
            }

            if (event.getActionCommand().matches("[\\+\\-*/]{1}")) {
                operator = event.getActionCommand();
                firstInput = this.show.getText();
                this.show.setText("");
            } else if (event.getActionCommand().equals("=")) {
                secondInput = this.show.getText();
                switch (operator) {
                    case "+":
                        this.show.setText("" + (Double.valueOf(firstInput) + Double.valueOf(secondInput)));
                        break;
                    case "-":
                        this.show.setText("" + (Double.valueOf(firstInput) - Double.valueOf(secondInput)));
                        break;
                    case "*":
                        this.show.setText("" + (Double.valueOf(firstInput) * Double.valueOf(secondInput)));
                        break;
                    case "/":
                        if (Double.valueOf(secondInput) != 0.0) {
                            this.show.setText("" + (Double.valueOf(firstInput) / Double.valueOf(secondInput)));
                        } else {
                            JOptionPane.showMessageDialog(this, "除数不能为零，请重新输入！");
                        }
                        break;
                }
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "输入有误，请重新输入");
        }
    }

    public static void main(String[] args) {
        new WindowsCalculation();
    }

}

/*if (event.getSource() == this.sign[0][0]) {
                //   if (event.getSource() == this.sign[4][2])                                          //取根号
                this.show.setText("" + Math.sqrt(Double.valueOf(show.getText())));
            } else if (event.getSource() == this.sign[0][1]) {   //取相反数
                this.show.setText((0 - Double.valueOf(show.getText())) + "");
            } else if (event.getSource() == this.sign[0][2]) {   //取模
                a = Double.valueOf(show.getText());
                this.show.setText("");
                if (this.sign[4][2].isSelected()) {
                    b = Double.valueOf(show.getText());
                    if (b == 0)
                        JOptionPane.showMessageDialog(this, "除数不能为零，请重新输入");
                    c = a % b;
                    this.show.setText(c + "");
                }
            } else if (event.getSource() == this.sign[0][3]) {   //退格
                this.show.setText(show.getText().substring(0, show.getText().length() - 1));
            } else if (event.getSource() == this.sign[1][0]) {
                this.show.setText(show.getText() + "7");
            } else if (event.getSource() == this.sign[1][1]) {
                this.show.setText(show.getText() + "8");
            } else if (event.getSource() == this.sign[1][2]) {
                this.show.setText(show.getText() + "9");
            } else if (event.getSource() == this.sign[1][3]) {   //"/"
                a = Double.valueOf(show.getText());
                this.show.setText("");
                if (event.getSource() == this.sign[4][2]) {
                    b = Double.valueOf(show.getText());
                    if (b == 0)
                        JOptionPane.showMessageDialog(this, "除数不能为零，请重新输入");
                    c = a / b;
                    this.show.setText(c + "");
                }
            } else if (event.getSource() == this.sign[2][0]) {
                this.show.setText(show.getText() + "4");
            } else if (event.getSource() == this.sign[2][1]) {
                this.show.setText(show.getText() + "5");
            } else if (event.getSource() == this.sign[2][2]) {
                this.show.setText(show.getText() + "6");
            } else if (event.getSource() == this.sign[2][3]) {   //"*"
                a = Double.valueOf(show.getText());
                this.show.setText("");
                if (event.getSource() == this.sign[4][2]) {
                    b = Double.valueOf(show.getText());
                    c = a * b;
                    this.show.setText(c + "");
                }
            } else if (event.getSource() == this.sign[3][0]) {
                this.show.setText(show.getText() + "1");
            } else if (event.getSource() == this.sign[3][1]) {
                this.show.setText(show.getText() + "2");
            } else if (event.getSource() == this.sign[3][2]) {
                this.show.setText(show.getText() + "3");

            } else if (event.getSource() == this.sign[3][3]) {  //"-"
                a = Double.valueOf(show.getText());
                this.show.setText("");
                if (event.getSource() == this.sign[4][2]) {
                    b = Double.valueOf(show.getText());
                    c = a - b;
                    this.show.setText(c + "");
                }
            } else if (event.getSource() == this.sign[4][0]) {
                this.show.setText(show.getText() + "0");
            } else if (event.getSource() == this.sign[4][1]) {
                this.show.setText(show.getText() + ".");   //假如输入的数字带有两个小数点，例如2.3.1，会抛出数值格式异常？？？
            } else if (event.getSource() == this.sign[4][2]) {   //"="

            } else if (event.getSource() == this.sign[4][3]) {  //"+"
                a = Double.valueOf(show.getText());
                this.show.setText("");
                if (event.getSource() == this.sign[4][2]) {
                    b = Double.valueOf(show.getText());
                    c = a + b;
                    this.show.setText(c + "");
                }
            }*/
          /* if (Integer.parseInt(event.getActionCommand())>=0&&Integer.parseInt(event.getActionCommand())<=9||
                   event.getActionCommand().equals(".")){
               this.show.setText(this.show.getText()+event.getActionCommand());
             a=Double.valueOf(this.show.getText());break;
            }*/